package buildcode.language.java.command;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import buildcode.build.AbstractSingleTableCommandExecuter;
import buildcode.config.EntityExportConfig;
import buildcode.database.DataBaseTable;
import buildcode.language.Type;
import buildcode.language.java.JavaUtil;
import buildcode.language.java.field.JavaField;
import buildcode.language.java.type.JavaEntity;

public class JavaEntityCommandExecute extends AbstractSingleTableCommandExecuter {

    @Override
    public List<Type> execute(DataBaseTable base, EntityExportConfig config) {
        JavaEntity entity = new JavaEntity(StringUtils.firstNonBlank(config.getFileName(), base.getCamelCaseName() + config.getSuffix()), config.getPackageName());
        base.getColumns().stream().forEach(column -> {
            entity.addField(new JavaField(column.getCamelCaseName(), JavaUtil.getJavaTypeByJdbcType(column.getJdbcType()), column.getNote(), column));
        });
        return Arrays.asList(entity);
    }

}
